package com.iflyun.rabbitmqcustmer.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.iflyun.rabbitmqcustmer.dao.WeChatProducerDao;
import com.iflyun.rabbitmqcustmer.domain.WeChatLog;



@Repository("weChatProducerDao")
public class WeChatProducerDaoImpl extends BaseDaoImpl<WeChatLog> implements WeChatProducerDao{
	
	@Resource(name="dataSource")
	private DataSource dataSource;

	@Override
	public int setWeChatLog(WeChatLog weChatLog) {
		
		this.save(weChatLog);
		
		return 1;
	}

	@Override
	public void saveWeChatLog(List<WeChatLog> list) {
		this.batchSave(list);
		
	}

	@Override
	public void setWeChatList(List<WeChatLog> list) {
		// TODO Auto-generated method stub
		JdbcTemplate jt= new JdbcTemplate(dataSource);
		final List<WeChatLog> weChatLogList = list;
		String sql="insert into t_log_detail_wx_temp values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		jt.batchUpdate(sql, new BatchPreparedStatementSetter(){
			public void setValues(PreparedStatement ps,int i)throws SQLException{
				String id=weChatLogList.get(i).getId();
				String userId=weChatLogList.get(i).getUserId();
				String ip=weChatLogList.get(i).getIp();
				String proxy_Ip=weChatLogList.get(i).getProxyIp();
				String openId=weChatLogList.get(i).getOpenId();
				String urlAbsoluteUrl=weChatLogList.get(i).getUrlAbsoluteUri();
				String jumpUrl=weChatLogList.get(i).getJumpUrl();
				String title=weChatLogList.get(i).getTitle();
				String physicalPath =weChatLogList.get(i).getPhysicalPath();
				String requestMode=weChatLogList.get(i).getRequestMode();
				String requestParameter = weChatLogList.get(i).getRequestParameter();
				String operationType = weChatLogList.get(i).getOperationType();
				String urlHost=weChatLogList.get(i).getUrlHost();
				String urlPort=weChatLogList.get(i).getUrlPort();
				String browseTime=weChatLogList.get(i).getBrowseTime();
				float GPS_X=weChatLogList.get(i).getGpsx();
				float GPS_Y=weChatLogList.get(i).getGpsy();
				String mobilePhoneBrand=weChatLogList.get(i).getMobilePhoneBrand();
				String operationSystem=weChatLogList.get(i).getOperationSystem();
				Timestamp accessTime=weChatLogList.get(i).getAccessTime();
				String productVersion=weChatLogList.get(i).getProductVersion();
				String productCode=weChatLogList.get(i).getProductCode();

		        ps.setString(1, id);
		        ps.setString(2, userId);
		        ps.setString(3, ip);
		        ps.setString(4, proxy_Ip);
		        ps.setString(5, openId);
		        ps.setString(6, urlAbsoluteUrl);
		        ps.setString(7, jumpUrl);
		        ps.setString(8, title);
		        ps.setString(9, physicalPath);
		        ps.setString(10, requestMode);
		        ps.setString(11, requestParameter);
		        ps.setString(12, operationType);
		        ps.setString(13, urlHost);
		        ps.setString(14, urlPort);
		        ps.setString(15, browseTime);
		        ps.setFloat(16, GPS_X);
		        ps.setFloat(17, GPS_Y);
		        ps.setString(18, mobilePhoneBrand);
		        ps.setString(19, operationSystem);
		        ps.setTimestamp(20, accessTime);
		        ps.setString(21, productVersion);
		        ps.setString(22, productCode);
		        
		       }
		       public int getBatchSize()
		       {
		        return weChatLogList.size();
		       }
			});
		
	}

}
